package com.atguigu.user.controller;

import com.atguigu.param.AddressListParam;
import com.atguigu.param.AddressRemonveParam;
import com.atguigu.pojo.Address;
import com.atguigu.user.service.AddressService;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Name: MyComputer
 * @ClassName AddressController
 * @Description:
 * @date 2023/5/18 20:06
 */
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/list")
    public R list(@RequestBody @Validated AddressListParam addressListParam, BindingResult result){

        if (result.hasErrors()){
            return R.fail("参数异常，查询失败");
        }

        return addressService.list(addressListParam.getUserId());
    }

    @PostMapping("/save")
    public R save(@RequestBody @Validated Address address, BindingResult result){

        if (result.hasErrors()){
            return R.fail("参数异常，保存失败");
        }

        return addressService.sava(address);
    }

    @DeleteMapping("/remove")
    public R remove(@RequestBody @Validated AddressRemonveParam addressRemonveParam, BindingResult result){

        if (result.hasErrors()){
            return R.fail("参数异常，删除失败");
        }

        return addressService.remove(addressRemonveParam.getId());
    }

}
